package com.hugang.juc.reference;


import java.lang.ref.WeakReference;

/**
 * java弱引用示例，只要发生gc，就回收
 *
 * 结果：
 * java.lang.Object@677327b6
 * java.lang.Object@677327b6
 * null
 * null
 *
 * @author hg
 * @date 2020/8/26 15:18
 */
public class WeakReferenceDemo {

    public static void main(String[] args) {

        Object o1 = new Object();
        WeakReference<Object> reference = new WeakReference<>(o1);

        System.out.println(o1);
        System.out.println(reference.get());

        o1 = null;
        System.gc();


        System.out.println(o1);
        System.out.println(reference.get());
    }
}
